/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.aptech.session.beans;

import entities.Event;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

/**
 *
 * @author Chien
 */
@Stateless
public class EventFacade extends AbstractFacade<Event> {

    @PersistenceContext(unitName = "BluePumpkin-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public EventFacade() {
        super(Event.class);
    }

    public List<Event> getAllActiveEvents(){
        String sql = "SELECT e FROM Event e WHERE  e.isActive = :isActive Order By e.startDate DESC";
        TypedQuery<Event> createQuery = getEntityManager().createQuery(sql, Event.class);      
        createQuery.setParameter("isActive", true);
        
        return createQuery.getResultList();
    }
    public List<Event> getUpcomingEvents() {
        List<Event> list ;
       Calendar cal = Calendar.getInstance();
        System.out.println("----TimeZone: "+cal.getTimeZone());     
       cal.add(Calendar.DATE, -1);
       
        System.out.println("--Current date:" +cal.getTime().toString());
        String sql = "SELECT e FROM Event e WHERE e.startDate >= :startRegDate AND e.isActive = :isActive Order By e.startDate ASC";
        TypedQuery<Event> createQuery = getEntityManager().createQuery(sql, Event.class);
        createQuery.setParameter("startRegDate", cal.getTime());
        createQuery.setParameter("isActive", true);
        
        list = createQuery.getResultList();
        if(list!=null){
            System.out.println("---found: "+list.size());
        }else{
            list = new ArrayList<Event>();
             System.out.println("---found: 0");
        }
        return list;
    }
    public List<Event> getCurrentEvents() {
        List<Event> list ;
      Calendar cal = Calendar.getInstance();
      
        String sql = "SELECT e FROM Event e WHERE e.startDate <= :startRegDate AND e.endDate >= :endRegDate AND e.isActive = :isActive";
        TypedQuery<Event> createQuery = getEntityManager().createQuery(sql, Event.class);
        createQuery.setParameter("startRegDate", cal.getTime());
        createQuery.setParameter("endRegDate",  cal.getTime());
         createQuery.setParameter("isActive", true);
        list = createQuery.getResultList();
//        if(list!=null){
//            System.out.println("getCurrentEvents---found: "+list.size());
//        }else{
//            list = new ArrayList<Event>();
//             System.out.println("getCurrentEvents---found: 0");
//        }
        return list;
    }
    public List<Event> getPastEvents(){
         List<Event> list ;
      Calendar cal = Calendar.getInstance();
      cal.set(Calendar.HOUR_OF_DAY, 0);
      cal.set(Calendar.MINUTE, 0);
      cal.set(Calendar.SECOND, 0);
        String sql = "SELECT e FROM Event e WHERE  e.endDate < :endRegDate AND e.isActive = :isActive";
        TypedQuery<Event> createQuery = getEntityManager().createQuery(sql, Event.class);
      
        createQuery.setParameter("endRegDate",  cal.getTime());
         createQuery.setParameter("isActive", true);
        list = createQuery.getResultList();
//        if(list!=null){
//            System.out.println("getCurrentEvents---found: "+list.size());
//        }else{
//            list = new ArrayList<Event>();
//             System.out.println("getCurrentEvents---found: 0");
//        }
        return list;
    }
}
